package com.zjy.controller;

import com.zjy.bean.ArticleTag;
import com.zjy.dto.ApiResult;
import com.zjy.service.ArticleTagService;
import com.zjy.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.zjy.api.Api.CLIENT_ERROR;
import static com.zjy.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/3/28.
 */
@Controller
@CrossOrigin
@RequestMapping("/articleTag")
public class ArticleTagController {
    @Resource
    private ArticleTagService articleTagService;

    /**
     * 新增文章标签
     * @param articleTag
     * @return
     */
    @RequestMapping(value = "/addArticleTag",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addArticleTag(@RequestBody ArticleTag articleTag){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(articleTag) || ObjectUtils.isNullOrEmpty(articleTag.getType())){
            result.error(CLIENT_ERROR,"缺少参数：type");
            return result;
        }else{
            //判断传入的type是否已存在，若已存在则提示换一个type值
            int flag = articleTagService.checkExistType(articleTag.getType());
            if(flag == 1){
                result.error(CLIENT_ERROR,"type值已存在");
                return result;
            }else{
                try{
                    articleTagService.addArticleTag(articleTag);
                    result.setMsg("文章标签插入成功！");
                }catch (Exception e){
                    result.error(SERVER_ERROR,e.getMessage());
                    return result;
                }
            }
        }
        return result;
    }

    /**
     * 根据Id软删除文章标签
     * @param articleTag
     * @return
     */
    @RequestMapping(value = "/deleteArticleTagById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteArticleTagById(@RequestBody ArticleTag articleTag){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(articleTag) || ObjectUtils.isNullOrEmpty(articleTag.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else{
            try{
                String tagId = articleTag.getId();
                int count = articleTagService.deleteArticleTagById(tagId);
                if(count == 0){
                    result.error(SERVER_ERROR,"文章标签删除失败！");
                    return result;
                }else if(count == 1){
                    result.setMsg("文章标签删除成功！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 根据Id更新文章标签
     * @param articleTag
     * @return
     */
    @RequestMapping(value = "/updateArticleTagById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateArticelTagById(@RequestBody ArticleTag articleTag){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(articleTag) || ObjectUtils.isNullOrEmpty(articleTag.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else{
            try{
                int count = articleTagService.updateArticleTagById(articleTag);
                if(count == 0){
                    result.error(SERVER_ERROR,"文章标签更新失败！");
                    return result;
                }else if(count == 1){
                    result.setMsg("文章标签更新成功！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 根据Id查找对应的文章标签
     * @param articleTag
     * @return
     */
    @RequestMapping(value = "/selectArticleTagById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectArticleTagById(@RequestBody ArticleTag articleTag){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(articleTag) || ObjectUtils.isNullOrEmpty(articleTag.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else{
            try{
                String id = articleTag.getId();
                ArticleTag articleTag1 = articleTagService.selectArticleTagById(id);
                if(ObjectUtils.isNullOrEmpty(articleTag1)){
                    result.setMsg("找不到此Id对应的文章标签");
                }else {
                    result.setData(articleTag1);
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 查找所有的文章标签
     * @return
     */
    @RequestMapping(value = "/selectAllArticleTag",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectAllArticleTag(){
        ApiResult result = new ApiResult();
        try{
            List<ArticleTag> list = articleTagService.selectAllArticleTag();
            if(ObjectUtils.isNullOrEmpty(list)){
                result.setMsg("找不到文章标签！");
            }else {
                result.setData(list);
            }
        }catch (Exception e){
            result.error(SERVER_ERROR,e.getMessage());
            return result;
        }
        return result;
    }
}
